package com.shycloud.mido.homework.controller;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Objects;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.homework.api.entity.MultiTeacherOfflineClass;
import com.shycloud.mido.homework.api.entity.MultiTeacherOfflineClassSetting;
import com.shycloud.mido.homework.api.vo.MultiTeacherOfflineClassSettingVO;
import com.shycloud.mido.homework.api.vo.MultiTeacherOfflineClassVO;
import com.shycloud.mido.homework.constant.Constant;
import com.shycloud.mido.homework.service.MultiTeacherOfflineClassService;
import com.shycloud.mido.homework.service.MultiTeacherOfflineClassSettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 *  线下课
 * </p>
 *
 * @author baiqiang
 * @since 2021-07-12
 */
@Slf4j
@Api(tags = "线下课")
@RestController
@AllArgsConstructor
@RequestMapping("/v1/offline")
public class MultiTeacherOfflineClassController {

	private MultiTeacherOfflineClassService multiTeacherOfflineClasservice;
	
    private MultiTeacherOfflineClassSettingService multiTeacherOfflineClassSettingService;
	
	@ApiOperation("查询进行中的线下课(不存在data为NULL)")
	@GetMapping("queryInProgress")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "teacherId", value = "老师ID", paramType = "query", dataType = "string",
				required = true),
		@ApiImplicitParam(name = "studentId", value = "学生ID", paramType = "query", dataType = "string",
				required = true)})
	public R queryOffineClass(MultiTeacherOfflineClassVO vo) {
		LambdaQueryWrapper<MultiTeacherOfflineClass> poLQW = Wrappers.<MultiTeacherOfflineClass>lambdaQuery()
		.eq(MultiTeacherOfflineClass::getStudentId, vo.getStudentId())
		.eq(MultiTeacherOfflineClass::getTeacherId, vo.getTeacherId())
		.orderByDesc(MultiTeacherOfflineClass::getCreateTime)
		.last("limit 1");
		MultiTeacherOfflineClass multiTeacherOfflineClass = multiTeacherOfflineClasservice.getOne(poLQW);
		
		if (Objects.isNull(multiTeacherOfflineClass)) {
			return new R();
		}
		
		LocalDateTime startTime = multiTeacherOfflineClass.getStartTime();
		Integer setTimeLength = multiTeacherOfflineClass.getSetTimeLength();
		LocalDateTime now = LocalDateTime.now();
		LocalDateTime endTime = startTime.plusSeconds(setTimeLength);
		if (now.isAfter(endTime)) {
			return new R();
		}
		return new R(multiTeacherOfflineClass);
	}
	
	@ApiOperation("线下课开始（如果进行中则返回当前线下课数据）")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "teacherId", value = "老师ID", paramType = "query", dataType = "string",
				required = true),
		@ApiImplicitParam(name = "studentId", value = "学生ID", paramType = "query", dataType = "string",
				required = true),
		@ApiImplicitParam(name = "setTimeLength", value = "设置默认时长", paramType = "query", dataType = "int",
				required = true)})
	@PostMapping("openSwitch")
	public R openSwitch(@RequestBody MultiTeacherOfflineClassVO vo) {
		//是否进行中 返回当前
		LambdaQueryWrapper<MultiTeacherOfflineClass> poLQW = Wrappers.<MultiTeacherOfflineClass>lambdaQuery()
				.eq(MultiTeacherOfflineClass::getStudentId, vo.getStudentId())
				.eq(MultiTeacherOfflineClass::getTeacherId, vo.getTeacherId())
				.orderByDesc(MultiTeacherOfflineClass::getCreateTime)
				.last("limit 1");
		MultiTeacherOfflineClass multiTeacherOfflineClass = multiTeacherOfflineClasservice.getOne(poLQW);
		//如果进行中则返回当前线下课数据
		if (Objects.nonNull(multiTeacherOfflineClass)) {
			LocalDateTime startTime = multiTeacherOfflineClass.getStartTime();
			Integer setTimeLength = multiTeacherOfflineClass.getSetTimeLength();
			LocalDateTime now = LocalDateTime.now();
			LocalDateTime endTime = startTime.plusSeconds(setTimeLength);
			if (!now.isAfter(endTime)) {
				return new R(multiTeacherOfflineClass);
			}
		}
		//创建线下课
		LocalDateTime now = LocalDateTime.now();
		MultiTeacherOfflineClass savePO = new MultiTeacherOfflineClass();
		BeanUtils.copyProperties(vo, savePO);
		savePO.setCreateTime(now);
		savePO.setUpdateTime(now);
		savePO.setDelFlag(Constant.DEL_FLAG);
		savePO.setStartTime(now);
		LocalDateTime endTime = now.plusSeconds(vo.getSetTimeLength());
		savePO.setEndTime(endTime);
		savePO.setClassData(now.toLocalDate());
		savePO.setActualTimeLength(vo.getSetTimeLength());
		multiTeacherOfflineClasservice.save(savePO);
		return new R();
	}
	
	@ApiOperation("线下课中断关闭（预留）")
	@GetMapping("closeSwitch")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "teacherId", value = "老师ID", paramType = "query", dataType = "string",
				required = true),
		@ApiImplicitParam(name = "studentId", value = "学生ID", paramType = "query", dataType = "string",
				required = true)
		})
	public R closeSwitch(MultiTeacherOfflineClassVO vo) {
		LambdaQueryWrapper<MultiTeacherOfflineClass> poLQW = Wrappers.<MultiTeacherOfflineClass>lambdaQuery()
				.eq(MultiTeacherOfflineClass::getStudentId, vo.getStudentId())
				.eq(MultiTeacherOfflineClass::getTeacherId, vo.getTeacherId())
				.orderByDesc(MultiTeacherOfflineClass::getCreateTime)
				.last("limit 1");
		MultiTeacherOfflineClass multiTeacherOfflineClass = multiTeacherOfflineClasservice.getOne(poLQW);
		
		if (Objects.isNull(multiTeacherOfflineClass)) {
			return new R(CommonConstants.FAIL, "线下课不存在无法终止", false);
		}
		
		LocalDateTime startTime = multiTeacherOfflineClass.getStartTime();
		Integer setTimeLength = multiTeacherOfflineClass.getSetTimeLength();
		LocalDateTime now = LocalDateTime.now();
		LocalDateTime endTime = startTime.plusSeconds(setTimeLength);
		if (now.isAfter(endTime)) {
			return new R(CommonConstants.FAIL, "线下课已经结束", false);
		}
		//进行中终端
		multiTeacherOfflineClass.setEndTime(now);
		long between = ChronoUnit.SECONDS.between(startTime, now);
		multiTeacherOfflineClass.setActualTimeLength((int)between);
		multiTeacherOfflineClass.setUpdateTime(now);
		multiTeacherOfflineClasservice.updateById(multiTeacherOfflineClass);
		return new R(true);
	}
	
	@ApiOperation("设置默认时长")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "teacherId", value = "老师ID", paramType = "query", dataType = "string",
				required = true),
		@ApiImplicitParam(name = "studentId", value = "学生ID", paramType = "query", dataType = "string",
				required = true),
		@ApiImplicitParam(name = "setTimeLength", value = "设置默认时长", paramType = "query", dataType = "int",
				required = true)})
	@PostMapping("saveDefaultSetting")
	public R saveDefaultSetting(@RequestBody MultiTeacherOfflineClassSettingVO vo) {
		LambdaQueryWrapper<MultiTeacherOfflineClassSetting> multiTeacherOfflineClassSettingLQW = Wrappers.<MultiTeacherOfflineClassSetting>lambdaQuery()
				.eq(MultiTeacherOfflineClassSetting::getTeacherId, vo.getTeacherId())
				.eq(MultiTeacherOfflineClassSetting::getStudentId, vo.getStudentId())
				.eq(MultiTeacherOfflineClassSetting::getDelFlag, Constant.DEL_FLAG);
				MultiTeacherOfflineClassSetting multiTeacherOfflineClassSetting = multiTeacherOfflineClassSettingService.getOne(multiTeacherOfflineClassSettingLQW);
		if (Objects.isNull(multiTeacherOfflineClassSetting)) {
			MultiTeacherOfflineClassSetting savePO = new MultiTeacherOfflineClassSetting();
			BeanUtils.copyProperties(vo, savePO);
			savePO.setCreateTime(LocalDateTime.now());
			savePO.setUpdateTime(LocalDateTime.now());
			savePO.setDelFlag(Constant.DEL_FLAG);
			multiTeacherOfflineClassSettingService.save(savePO);
		}else {
			multiTeacherOfflineClassSetting.setSetTimeLength(vo.getSetTimeLength());
			multiTeacherOfflineClassSettingService.updateById(multiTeacherOfflineClassSetting);
		}
		return new R(true);
	}
	
	@ApiOperation("查询设置默认时长")
	@GetMapping("queryDefaultSetting")
	public R queryDefaultSetting(MultiTeacherOfflineClassSettingVO vo) {
		LambdaQueryWrapper<MultiTeacherOfflineClassSetting> multiTeacherOfflineClassSettingLQW = Wrappers.<MultiTeacherOfflineClassSetting>lambdaQuery()
		.eq(MultiTeacherOfflineClassSetting::getTeacherId, vo.getTeacherId())
		.eq(MultiTeacherOfflineClassSetting::getStudentId, vo.getStudentId())
		.eq(MultiTeacherOfflineClassSetting::getDelFlag, Constant.DEL_FLAG);
		MultiTeacherOfflineClassSetting multiTeacherOfflineClassSetting = multiTeacherOfflineClassSettingService.getOne(multiTeacherOfflineClassSettingLQW);
		return new R(multiTeacherOfflineClassSetting);
	}
	
	
}
